package com.shd.edanpei.presenter;

import com.shd.edanpei.base.BasePresenterImpl;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.exception.ResponeThrowable;
import com.shd.edanpei.base.network.model.NoDataResponse;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.bean.PagingModel;
import com.shd.edanpei.bean.QuitTeamBean;
import com.shd.edanpei.presenter.contract.QuitApprovalContract;
import com.shd.edanpei.util.ToastUtil;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by mc on 2019/1/2.
 * yang-wen-wen
 */

public class QuitApprovalPresenter extends BasePresenterImpl<QuitApprovalContract.QuitApprovalIView>
        implements QuitApprovalContract.QuitApprovalIPresenter {

    private int currentPage;
    private int totalPage;

    public QuitApprovalPresenter(QuitApprovalContract.QuitApprovalIView mView) {
        super(mView);
    }

    @Override
    public void refresh(int teamID) {
        currentPage = 1;
        ApiCreator.getApi().quitList(teamID, currentPage)
                .compose(RxTransformer.<PagingModel<QuitTeamBean>>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<PagingModel<QuitTeamBean>>() {
                    @Override
                    protected void onSuccess(PagingModel<QuitTeamBean> data) {
                        if (data != null && data.getList().size() > 0) {
                            totalPage = data.getTotalPage();
                            mBaseIView.getAdapter().setNewData(data.getList());
                            if (currentPage >= totalPage) {
                                mBaseIView.getAdapter().loadMoreEnd();
                            } else {
                                mBaseIView.getAdapter().loadMoreComplete();
                            }
                            currentPage++;
                        } else {
                            mBaseIView.getAdapter().setNewData(null);
//                            mBaseIView.getAdapter()
//                                    .setEmptyView(LayoutUtils.getNullLayout(mBaseIView.getContext(), R.drawable.pic_null_bg, "暂无相关订单"));
                        }
                    }
                });
    }

    @Override
    public void loadMore(int teamID) {
        ApiCreator.getApi().quitList(teamID, currentPage)
                .compose(RxTransformer.<PagingModel<QuitTeamBean>>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<PagingModel<QuitTeamBean>>() {
                    @Override
                    protected void onSuccess(PagingModel<QuitTeamBean> data) {
                        if (data != null && data.getList().size() > 0) {
                            mBaseIView.getAdapter().addData(data.getList());
                            if (currentPage >= totalPage) {
                                mBaseIView.getAdapter().loadMoreEnd();
                            } else {
                                mBaseIView.getAdapter().loadMoreComplete();
                            }
                            currentPage++;
                        } else {
                            mBaseIView.getAdapter().loadMoreEnd();
                        }
                    }

                    @Override
                    public void onFailure(ResponeThrowable throwable) {
                        mBaseIView.getAdapter().loadMoreEnd();
                    }
                });
    }

    @Override
    public void argee(int teamId, final int position) {
        mBaseIView.showLoading("");
        ApiCreator.getApi().argeeQuit(teamId)
                .compose(RxTransformer.<NoDataResponse>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<NoDataResponse>() {
                    @Override
                    protected void onSuccess(NoDataResponse data) {
                        ToastUtil.showToast("审批成功");
                        mBaseIView.getAdapter().remove(position);
                    }

                    @Override
                    public void onFinish() {
                        mBaseIView.stopLoading();
                    }
                });
    }
}
